﻿using System.Collections.Generic;

namespace Tuxedo.Compile
{
	internal class UsingDirective
	{
		public string FullyQualifiedPath { get; set; }
		public string Alias { get; set; }

		public UsingDirective(string fullyQualifiedPath, string nullableAlias)
		{
			this.FullyQualifiedPath = fullyQualifiedPath;
			this.Alias = nullableAlias;
		}

		public static UsingDirective Parse(TokenStream tokens)
		{
			tokens.PopExpected("using");
			string first = tokens.PopIdentifier();
			string alias = null;
			if (tokens.PopIfPresent("="))
			{
				alias = first;
				first = tokens.PopIdentifier();
			}

			List<string> path = new List<string>() { first };
			while (!tokens.PopIfPresent(";"))
			{
				tokens.PopExpected(".");
				path.Add(tokens.PopIdentifier());
			}

			return new UsingDirective(string.Join(".", path), alias);
		}
	}
}
